<template>
  <div class="input-div">
    <!-- <div class="row">
      <a-input placeholder="请输入验证码" v-model="code" allow-clear></a-input>
      <a-button type="primary" @click="sendCode" class="send-btn" :disabled="sendStatus === $enums.SendCodeStatus.done" :loading="sendStatus === $enums.SendCodeStatus.doing">{{ btnText }}</a-button>
    </div> -->
    <c-send-code class="row" :phonenumber="currentUser.phonenumber" ref="sendCode" v-model="code" @afterSend="getCodeInfo"></c-send-code>
    <div class="row">
      <a-input-password placeholder="请输入新密码" v-model="password" allow-clear></a-input-password>
    </div>
  </div>
</template>

<script lang="ts">
import Base from "@/controllers/base.ts";
import { Component } from "vue-property-decorator";
import { Input, Button } from "ant-design-vue";
// import CSendCode from "@/components/cSendCode.vue";
import CSendCode from "@/components/CSendCode/index.vue";

@Component({
  name: "ByCode",
  components: {
    CSendCode,
    AButton: Button,
    AInput: Input,
    AInputPassword: Input.Password
  }
})
export default class ByCode extends Base {
  private code = "";
  private uuid = "";
  private password = "";

  private beforeSave() {
    this.$message.destroy();
    if (!this.uuid) {
      this.$message.error("请先发送验证码");
      return false;
    }

    if (!this.code) {
      this.$message.error("请填写验证码");
      return false;
    }

    if (!this.password) {
      this.$message.error("请填写新密码");
      return false;
    }

    return true;
  }

  private async save() {
    if (this.beforeSave()) {
      const res = await this.$api.UpdatePwdByCode(this.params);

      if (res && res.code === 200) {
        this.init();
        return Promise.resolve(true);
      }
    }
    return Promise.resolve(false);
  }

  // 初始化修改手机弹框
  private init() {
    this.uuid = "";
    this.code = "";
    this.password = "";
  }

  /**
   * 发送完短信验证码后的回调函数，用来获取接口返回的相关数据
   */
  private getCodeInfo(obj: any) {
    this.uuid = obj.uuid;
  }

  private get params() {
    return {
      password: this.password,
      phonenumber: this.currentUser.phonenumber,
      code: this.code,
      uuid: this.uuid
    };
  }
}
</script>

<style lang="less" scoped></style>
